﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;

public partial class QL_Dien_Thoai : System.Web.UI.Page
{
    SqlConnection con;
    SqlCommand cmd;
    DataTable dt = new DataTable();
    private string Connect = ConfigurationManager.ConnectionStrings["ketnoi"].ConnectionString;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["username"].ToString() == "")
        {
            Response.Redirect("Trang_Chu.aspx");
        }
    }
    protected void btnthem_Click(object sender, EventArgs e)
    {
        lbl.Text = Label1.Text = "";
        con = new SqlConnection(Connect);
        try
        {
            con.Open();
            cmd = new SqlCommand();
            cmd.Connection = con;
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = "themdienthoai";
            cmd.Parameters.AddWithValue("@TenSp", txttendt.Text);
            cmd.Parameters.AddWithValue("@SoLuong", txtsoluong.Text);
            cmd.Parameters.AddWithValue("@KieuDang", DropDownListKieuDang.Text);
            cmd.Parameters.AddWithValue("@tenhang", DropDownListmaHang.Text);
            cmd.Parameters.AddWithValue("@NamSX", txtnamsx.Text);
            cmd.Parameters.AddWithValue("@Gia", txtgia.Text);
            cmd.Parameters.AddWithValue("@GiaKM", txtgiakm.Text);
            cmd.Parameters.AddWithValue("@Hinh", txtHinh.Text);
            cmd.Parameters.AddWithValue("@MP3", RroCoMp3.Checked);
            cmd.Parameters.AddWithValue("@Camera", DropDownListCamera.Text);
            cmd.Parameters.AddWithValue("@Bluetooth", RroCoBl.Checked);
            cmd.Parameters.AddWithValue("@G3", RroCo3g.Checked);
            cmd.Parameters.AddWithValue("@Wifi", DropDownListwifi.Text);
            cmd.Parameters.AddWithValue("@GPRS", RroCoRp.Checked);
            cmd.Parameters.AddWithValue("@XemPhim", DropDownListXemPhim.Text);
            cmd.Parameters.AddWithValue("@FMRadio", RroCoFm.Checked);
            cmd.Parameters.AddWithValue("@XemTiVi", RroCoTivi.Checked);
            cmd.Parameters.AddWithValue("@Game", txtgame.Text);
            cmd.Parameters.AddWithValue("@LoaNgoai", RroCoLoa.Checked);
            cmd.Parameters.AddWithValue("@NhacChuong", txtnhacchuong.Text);
            cmd.Parameters.AddWithValue("@TheNho", txtthenho.Text);
            cmd.Parameters.AddWithValue("@BoNhoTrong", txtbonhotrong.Text);
            cmd.Parameters.AddWithValue("@BangTan", DropDownListBangtan.Text);
            cmd.Parameters.AddWithValue("@MangSuDung", DropDownListmangUse.Text);
            cmd.Parameters.AddWithValue("@BaoHanh", DropDownListbaohang.Text);
            cmd.Parameters.AddWithValue("@CacTinh_NK", txtcactinhnangkh.Text);
            cmd.Parameters.AddWithValue("@Ngay_cap_nhat", txtngaycapnhat.Text);
            cmd.Parameters.AddWithValue("@Hinh2", lblhinh2.Text);
            int i;
            i = cmd.ExecuteNonQuery();
            if (i > 0)
            {
                Label1.Text = "Thêm thành công";
            }
            else
            {
                Label1.Text = "Thêm thất bại";
            }          
        }
        catch 
        {
            string sql1 = "select TenSP from San_pham";
            con = new SqlConnection(Connect);
            cmd = new SqlCommand();
            cmd.Connection = con;
            cmd.CommandType = CommandType.Text;
            cmd.CommandText = sql1;
            try
            {
                con.Open();
                SqlDataReader reader = cmd.ExecuteReader();
                while (reader.Read())
                {
                    if (reader[0].ToString() == txttendt.Text)
                    {
                        lbl.Text = "lỗi tên điện thoại";
                    }
                }      
            }
            catch { }
        } 
    }
    
    protected void btnUpHinh_Click(object sender, EventArgs e)
    {
        HttpPostedFile file = FileUpload1.PostedFile;
        if (FileUpload1.HasFile == false || file.ContentLength > 800000)
        {
            lblErr.Text = "Chọn file ảnh khác";
        }
        else
        {
            try
            {
                string strPath = Server.MapPath("~/images/" + FileUpload1.FileName);
                FileUpload1.SaveAs(strPath);
                txtHinh.Text = "~/images/" + FileUpload1.FileName.ToString();
                lblhinh2.Text = "images/" + FileUpload1.FileName.ToString();
                lblErr.Text = "Upload thành công";
            }
            catch
            {
                lblErr.Text = "Upload thất bại";
            }
        }
    }
    protected void btnxoa_Click(object sender, EventArgs e)
    {
        lblErr.Text = "";
        con = new SqlConnection(Connect);
        try
        {
            con.Open();
            cmd = new SqlCommand();
            cmd.Connection = con;
            cmd.CommandText = "delete from SAN_PHAM where TenSp = '" + DropDownList1.Text + "'";
            int i;
            i = cmd.ExecuteNonQuery();
            if (i > 0)
            {
                lbl.Text = "Xóa thành công";

            }
            else
            {
                lbl.Text = "Xóa không thành công";
            }
        }
        catch
        {
            lblErr.Text = "Lỗi";
        }
    }
}
